package com.example.test_witns;

import org.junit.Test;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

public class Client {

    @Test
    public void mainSocket() {
        try {
            // 创建 Socket 连接到服务端
            Socket socket = new Socket("104.43.113.253", 12345);
            
            // 发送消息到服务端
            PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
            out.println("你好，服务端！");

            // 接收服务端的响应
            BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            String response = in.readLine();
            System.out.println("服务端响应：" + response);

            // 关闭连接
            in.close();
            out.close();
            socket.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}